<template>
  <div class="bookshelf">
    <heads />
    <div class="login" v-if="!flg">
      <div class="zt">未登录</div>
      <router-link class="loginBtn" tag="div" to="/login">登录</router-link>
    </div>
    <div class="recent_reading">
      <div class="navs">我的书架</div>
      <div class="reading_box">
        <div class="none" v-if="!flg">还木有任何书籍</div>
        <div class="listData" v-for="item in userData" :key="item.id">
          <div class="item" @click="goBook(item.bookId, item.title)">
            <div class="img">
              <img :src="item.cover" alt="" width="100%" height="100%" />
            </div>
            <div class="txt">
              <div class="title">{{ item.title }}</div>
              <div class="name">{{ item.author }}</div>
              <div class="context">1999万人阅读 | 已完结</div>
              <div class="van-multi-ellipsis--l2 p">
                {{ item.content }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getBookData } from "../api/bookshelf";
import heads from "../components/heads.vue";
export default {
  components: { heads },
  data() {
    return {
      flg: null,
      userData: [],
    };
  },
  methods:{
    goBook(id, name) {
      console.log(id, name);
      this.$router.push({
        path: "/bookDetails",
        query: { id: id, name: name },
      });
    },
  },
  created() {
    let token = window.localStorage.getItem("token");
    this.flg = token;
    let user = JSON.parse(window.localStorage.getItem("user"));
    if (user) {
      user.forEach((item) => {
        if (item.username == token) {
          if (item.userBooks) {
            item.userBooks.forEach((i) => {
              getBookData({
                sourceUuid: i.bookId,
              }).then((data) => {
                console.log(data );
                this.userData.push(data.data.book)
              });
            });
          }
        }
      });
    }
  },
};
</script>

<style lang="less">
.bookshelf {
  height: 100%;
  overflow: hidden;
  .login {
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px;
    background-color: #fff;
    border-bottom: 1px solid #ccc;
    .zt {
      font-size: 14px;
      color: #666666;
    }
    .loginBtn {
      background-color: orange;
      height: 30px;
      padding: 0 8px;
      border-radius: 6px;
      line-height: 30px;
      color: #fff;
    }
  }
  .recent_reading {
    height: 75%;
    .navs {
      text-align: center;
      height: 35px;
      line-height: 35px;
      font-size: 14px;
      color: red;
      border-bottom: 4px solid red;
    }
    .reading_box {
      height: 100%;
      width: 100%;
      background-color: #fff;
      position: relative;
      .none {
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0px;
        top: 0px;
        margin: auto;
        width: 112px;
        height: 30px;
      }
      .listData {
        .item {
          padding: 15px;
          display: flex;
          border-bottom: #dcdcdc solid 1px;
          .img {
            width: 64px;
            height: 88px;
            margin-right: 10px;
            background-color: pink;
            flex-shrink: 0;
          }
          .txt {
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            .title {
              font-size: 16px;
              color: #1d2c33;
            }
            .name,
            .context,
            .p {
              font-size: 12px;
              color: #aaa;
            }
          }
        }
      }
    }
  }
}
</style>